<template>
  <el-card class="register-card">
    <h2 class="title">注册</h2>
    <el-form :model="form" ref="registerForm" label-position="top" status-icon>
      <el-form-item label="学号" prop="studentNo" :rules="[{ required: true, message: '请输入学号', trigger: 'blur' }]">
        <el-input v-model="form.studentNo" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
        <el-input v-model="form.password" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="姓名" prop="name" :rules="[{ required: true, message: '请输入姓名', trigger: 'blur' }]">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="班级号" prop="classId" :rules="[{ required: true, message: '请输入班级号', trigger: 'blur' }]">
        <el-input v-model="form.classId" autocomplete="off" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onRegister">注册</el-button>
        <el-button type="text" @click="$router.push('/login')">返回登录</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import { reactive } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';

const form = reactive({
  studentNo: '',
  password: '',
  name: '',
  classId: ''
});

const onRegister = async () => {
  if (!form.studentNo || !form.password || !form.name || !form.classId) {
    ElMessage.error('请完整填写学号、密码、姓名和班级号');
    return;
  }
  try {
    await axios.post('http://localhost:8081/api/students/register', form);
    ElMessage.success('注册成功，请登录');
    window.location.href = '/login';
  } catch (error) {
    ElMessage.error(error.response?.data?.message || '注册失败');
  }
};
</script>

<style scoped>
.register-card {
  max-width: 400px;
  margin: 100px auto;
  padding: 30px;
  border-radius: 8px;
}
.title {
  text-align: center;
  font-weight: 700;
  font-size: 24px;
  margin-bottom: 20px;
}
</style>
